<template>
  <div class="question-container">
    <el-card>
      <!-- 搜索 -->
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字:">
              <el-input
                v-model="requestParams.keyword"
                style="width: 190px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="12">
            <el-form-item style="text-align: right">
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="filter()">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom: 15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      />
      <!-- 表格 -->
      <el-table :data="randoms">
        <el-table-column label="编号" prop="id" />
        <el-table-column label="题型">
          <template #default="{ row }">
            {{
              questionType.find((item) => item.value === +row.questionType)
                .label
            }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号">
          <template #default="{ row }">
            <div v-for="item in row.questionIDs" :key="item.number">
              <a
                href="#"
                style="color: #09f"
                @click.prevent="openPreviewDialog(item)"
              >{{ item.number }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" width="160px">
          <template #default="{ row }">{{
            row.addTime | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds" />
        <el-table-column
          label="正确率(%)"
          prop="accuracyRate"
        />
        <el-table-column label="录入人" prop="userName" />
        <el-table-column label="操作" width="240px">
          <template #default="{ row }">
            <el-button icon="el-icon-delete" circle @click="del(row)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[5, 10, 20, 50]"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <!-- 预览 -->
    <questions-preview :is-show-dialog.sync="isShowDialog" :row-question="rowQuestion" />
  </div>
</template>
<script>

import QuestionsPreview from '../components/questions-preview'
import { questionType, difficulty } from '@/api/hmmm/constants'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
export default {
  components: {
    QuestionsPreview
  },
  data() {
    return {
      isShowDialog: false,
      rowQuestion: {},
      questionType,
      difficulty,
      randoms: [],
      total: 0,
      requestParams: {
        page: 1,
        pagesize: 20,
        keyword: ''
      }
    }
  },
  created() {
    this.getRandoms()
  },
  methods: {
    async getRandoms() {
      const { data } = await randoms(this.requestParams)
      console.log(data)
      this.randoms = data.items
      this.total = data.counts
    },
    handleCurrentChange(value) {
      this.requestParams.page = value
      this.getRandoms()
    },
    handleSizeChange(value) {
      this.requestParams.pagesize = value
      this.requestParams.page = 1
      this.getRandoms()
    },
    filter() {
      this.requestParams.page = 1
      this.getRandoms()
    },
    clear() {
      this.requestParams.keyword = ''
      this.getRandoms()
    },
    del(id) {
      this.$confirm('此操作将永久删除此题组，是否继续', '提示', {
        type: 'warning'
      })
        .then(async() => {
          const res = await removeRandoms(id)
          console.log(res)
          this.$message.success('删除题组成功')
          this.getRandoms()
        })
        .catch(() => {
          console.log('取消')
        })
    },
    openPreviewDialog(item) {
      this.isShowDialog = true
      this.rowQuestion = item
    }
  }
}
</script>

<style>

</style>
